<?php
include_once 'ctype/field/upload.php';

/*
 * data: array(
 * 	'path' 			=> '....',
 * 	'width'			=> 100
 * 	'height			=> 90 
 * 	'other_info'	=> ...,
 * 
 * 	'thumbnails'	=> array(
 * 		'min1'	=> array(
 * 			'path' 			=> '.....',
 * 			'width'			=> 100,
 * 			'height'		=> 100,
 * 			'other_info'	=> ...,
 * 		)
 * 	)
 * )
 */
class Imagen extends Upload{
	var $template = "ctype/views/fields/imagen.php";
	
	function Imagen(){
		parent::Upload();
		
		$this->properties["upload_allow_filetypes"] = "jpg|png|jpeg|gif";
		$this->properties["upload_allow_filetypes_msg"]	= "Solo JPG, PNG y GIF son validos";
		
		$this->properties['thumbnails'] = array(
			"thumb_100_100" => array(
				"width"		=> 200,
				"height"	=> 100
			),
			"resize_crop" => array(
				"width"		=> 200,
				"height"	=> 100,
				"action"	=> "resizecrop",
			)
		);
		
		$this->ci->load->library('image_lib'); 
		
	}
	
	function createFileData ($filePath){
		$data = parent::createFileData($filePath);
		
		if($data != NULL){
			$data['thumbnails'] = array();
			
			if(isset($this->properties['thumbnails']) && $this->properties['thumbnails'] != NULL){
				foreach($this->properties['thumbnails'] as $thumb_name=>$thumb_config){
					$thumb_config['name'] = $thumb_name;
					$thumb_config['source_file'] = $filePath;
					
					$thumb_data = $this->generate_thumbnail($thumb_config);
					
					if($thumb_data != NULL){
						$data['thumbnails'][$thumb_name] = $thumb_data;
					}
				}
			}
			
		}
		
		return $data;
	}
	
	
	function generate_thumbnail($config = array()){
		$thumbnail = NULL;
		
		if(!isset($config['source_file']) || !file_exists($config['source_file'])){
			log_error("error", "No existe fichero para crear thumbnail");
			return NULL;
		}
		
		if(!isset($config['name'])){
			log_error("error", "No ha seleccionado nombre para el thumbnail");
			return NULL;
		}
		
		$defaults = array(
			"action"			=> "resize",
			"width"				=> 100,
			"height"			=> 100,
			"maintain_ratio"	=> TRUE,
			"quality"			=> 90,
			"master_dim"		=> "auto",
		);
		
		$config = array_merge($defaults, $config);
		
		if($config['action'] == "resizecrop"){
			$config['maintain_ratio'] = FALSE;
		}
		
		
		$pathinfo = pathinfo($config['source_file']);
		
		$fileDest = $pathinfo['filename'] . "_" . $config['name'] . "." . $pathinfo['extension'];

		$result = TRUE;
		//Comprobamos si existiera la imagen para no tratarla de nuevo
		if(!file_exists($pathinfo['dirname'] . "/" . $fileDest)){
		
			$thumb_config['image_library'] = 'gd2';
			$thumb_config['source_image'] = realpath($config['source_file']);
			$thumb_config['new_image'] = $fileDest;
			
			$thumb_config['maintain_ratio'] = $config["maintain_ratio"];
			$thumb_config['width']	 = $config["width"];
			$thumb_config['height']	= $config["height"];
			$thumb_config['quality'] = $config["quality"];
			$thumb_config['master_dim']	= $config["master_dim"];
			
			$this->ci->image_lib->clear();		
			$this->ci->image_lib->initialize($thumb_config);
			
			$result = FALSE;
			if($config['action'] == "resize"){
				$result = $this->ci->image_lib->resize();
			} else if($config['action'] == "resizecrop"){
				$result = $this->ci->image_lib->resizeThenCrop();
			}
			
		}
		
		if ($result) {
			$thumbnail = array();
			
			$thumbnail['path'] = $pathinfo['dirname'] . "/" . $fileDest;
			$thumbnail['size'] = filesize($pathinfo['dirname'] . "/" . $fileDest);
			$thumbnail['type'] = filetype($pathinfo['dirname'] . "/" . $fileDest);
			
		} else{
			log_message("error", "Error tratando la imagen " . $config['name']);
		}
		
		return $thumbnail;
	}
	
	
	function delete_data_file($data){
		unlink($data['path']);
		
		Console::log("Eliminando data : ".print_r($data, TRUE));
		
		if(isset($data['thumbnails'])){
			foreach($data['thumbnails'] as $thumbnail_data){
				unlink($thumbnail_data['path']);
			}
		}
	}
}
?>